package com.vcc.boss.core.component;

import com.vcc.boss.core.config.AccountConfig;
import com.vcc.core.exception.ProgramException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
public class UserComponent {


    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private AccountConfig accountConfig;


    public String getToken(Long userId) {
        String url = "http://"+ accountConfig.getIp() +"/token/getRoToken?userId=" + userId;
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        HttpHeaders headers = response.getHeaders();
        List<String> tokens = headers.get("token");
        if (tokens == null || tokens.isEmpty()) {
            throw new ProgramException("生成ro环境token失败");
        }
        return tokens.get(0);
    }
}
